/////////////
//	CBDrawM.cpp		:	v0010
//	Written by		:	Liu Gang
//	Compiler		:	Microsoft Visual C++ 4.2 & DirectX
//	Library			:	DDraw.Lib
//  Copyright		:	WayAhead Software Co.Ltd.	1996-1997
//	v0010			:	Feb.9.1997
//	v0011			:	May.15.1997, changed FilpWater()
/////////////
// implementation file
// misc functions in drawing
// copy screen, to make screen shots

#include "stdafx.h"
#include "Assert.h"
#include "DDCompo.h"	// Direct Draw objects and components

#include "CBDraw.h"
#include <stdio.h>

// global
extern CDDPalette palMain;

////////////////
// copy back buffer to a file named "ScreenXX.bmp"
// to make screen shots, advertising
// true if succeeded
BOOL DRAW_CopyScreen()
{
	FILE * fp = NULL;
	
	// open file
	char filename[65];
	int i=0;
	do
	{
		strcpy( filename, "Screen" );
		char tmp[32];
		wsprintf( tmp, "%02d", i );
		strcat( filename, tmp );
		strcat( filename, ".bmp" );
		i++;
		if( fp )	fclose( fp );
	}while( (fp = fopen( filename, "rb" )) != NULL );

	fp = fopen( filename, "wb" );
	if( !fp )
	{
		OutputDebugString( "DRAW_CopyScreen Error(0): Cannot open file named: " );
		OutputDebugString( filename );
		OutputDebugString( "\n" );
		return FALSE;
	}

	// prepare headers to write to bitmap
	BITMAPFILEHEADER	fileheader;
	BITMAPINFOHEADER	bmiheader;
	PALETTEENTRY		ape[256];

	memset( &fileheader, 0 , sizeof( fileheader ) );
	memset( &bmiheader, 0, sizeof( bmiheader ) );
	memset( ape, 0, sizeof( ape ) );

	fileheader.bfType = 0x4D42;
	fileheader.bfOffBits = sizeof(fileheader)+sizeof(bmiheader)+sizeof(ape);
	fileheader.bfSize = fileheader.bfOffBits+SCREEN_WIDTH*SCREEN_HEIGHT;
	
	bmiheader.biSize = sizeof( bmiheader );
	bmiheader.biWidth = SCREEN_WIDTH;
	bmiheader.biHeight = SCREEN_HEIGHT;
	bmiheader.biPlanes = 1;
	bmiheader.biBitCount = 8;
	bmiheader.biCompression = BI_RGB;
	bmiheader.biSizeImage = SCREEN_WIDTH*SCREEN_HEIGHT;

	if( DD_GetPalette()->GetEntries( 0, 0, 256, ape ) != DD_OK )
	{
		OutputDebugString( "DRAW_CopyScreen Error(1): cannot get palette from system!\n" );
		fclose( fp );
		return FALSE;
	}
	// flip palette entries
    for(i=0; i<256; i++ )
    {
        BYTE r = ape[i].peRed;
        ape[i].peRed  = ape[i].peBlue;
        ape[i].peBlue = r;
    }
	
	// write data
	// write headers
	if( fwrite( &fileheader, sizeof(fileheader), 1, fp ) != 1 )
	{
		OutputDebugString( "DRAW_CopyScreen Error(2): error in writing file header!\n" );
		fclose( fp );
		return FALSE;
	}
	if( fwrite( &bmiheader, sizeof(bmiheader), 1, fp ) != 1 )
	{
		OutputDebugString( "DRAW_CopyScreen Error(3): error in writing bitmap info header!\n" );
		fclose( fp );
		return FALSE;
	}
	if( fwrite( ape, sizeof(ape), 1, fp ) != 1 )
	{
		OutputDebugString( "DRAW_CopyScreen Error(4): error in writing palette!\n" );
		fclose( fp );
		return FALSE;
	}

	// write map data
	LPDIRECTDRAWSURFACE2 lpSurface = DD_GetBackBuffer();
	DDSURFACEDESC       ddsd;
	ddsd.dwSize = sizeof( ddsd );
	
	Assert( lpSurface );
	if( lpSurface->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) == DD_OK )
	{
		BYTE *pSrc = (BYTE*)ddsd.lpSurface;
		int widthSrc = ddsd.dwWidth;
		int heightSrc = ddsd.dwHeight;

		pSrc += widthSrc*(heightSrc-1);
		for( int i=0; i<heightSrc; i++ )
		{
			if( fwrite( pSrc, widthSrc, 1, fp ) != 1 )
			{
				OutputDebugString( "DRAW_CopyScreen Error(5): error in writing map data!\n" );
				fclose( fp );
				return FALSE;
			}
			pSrc -= widthSrc;
		}
		lpSurface->Unlock( NULL );
	}

	// close file
	fclose( fp );
	return TRUE;
}
////////////////

////////////////
#define	DRAW_FLIPWATER_DELAY	6
// draw blinking water, change palette to perform it
//PALETTEENTRY colorWater[6]={{0,10,70,0},{0,25,85,0},{5,40,100,0},{10,55,120,0},{15,70,135,0},{20,85,150,0}};
PALETTEENTRY colorWater[4]={{0,25,85,0},{5,40,100,0},{10,55,120,0},{15,70,135,0}};
void DRAW_FlipWater()
{
	static int timedelay=0;
	timedelay++;
	if( timedelay< DRAW_FLIPWATER_DELAY )	return;
	
	timedelay=0;

	// get palette
	PALETTEENTRY	ape[256];
	if( DD_GetPalette()->GetEntries( 0, 0, 256, ape ) != DD_OK )
	{
		OutputDebugString( "DRAW_FlipWater Error(0): Cannot get palette object!\n" );
		return;
	}

	int nDest[3] = {101,102,103};
	int nSrc[6] = {0,1,2,3,2,1};
	static int nCounter=0;

	memcpy( &ape[nDest[0]], &colorWater[nSrc[(0+nCounter)%6]], sizeof( PPALETTEENTRY ) );
	memcpy( &ape[nDest[1]], &colorWater[nSrc[(1+nCounter)%6]], sizeof( PPALETTEENTRY ) );
	memcpy( &ape[nDest[2]], &colorWater[nSrc[(2+nCounter)%6]], sizeof( PPALETTEENTRY ) );
	nCounter++;

	// set palette
	if( DD_GetPalette()->SetEntries(0, 0, 256, ape) != DD_OK )
	{
		OutputDebugString( "DRAW_FlipWater Error(1): Cannot set palette object!\n" );
		return;
	}
}
////////////////

